package com.yq.servlet;


import com.yq.bean.Product;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/item")
public class ServletItem extends HttpServlet {



    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        String id = request.getParameter("id");
        if (id==null || "".equals(id)){
            response.setHeader("refresh","url="+request.getContextPath()+"/index"+"");
            return;
        }
        List<Product> productsList = (List<Product>) getServletContext().getAttribute("products");
        for (Product product : productsList) {
            if (id.equals(product.getId())){
                response.getWriter().println(product);
            }
        }
        response.getWriter().println("<a href="+request.getContextPath()+"/addCard?id="+id+">加入购物车</a>");
        response.getWriter().println("<a href="+request.getContextPath()+"/view"+">查看购物车</a>");
        response.getWriter().println("<a href="+request.getContextPath()+"/index"+">加入购物车</a>");



    }
}
